classdef ET_ANALYSIS < ET_GEN

    properties
        edffile   
    end
    
    methods 
       
        function self = ET_ANALYSIS(edfFileInstance)
            self = self@ET_GEN();
            self.num_blocks = 1;
            self.num_trials = 1;
            % Populate the experiment
            self.prepareTrialTypes()
            self.prepareOccluder()
            self.prepareSpeeds()
            self.prepareJump()
            self.prepareFixation();
            % Populate with the edfFile information
            parameters = edfFileInstance.parametersReal();
            self.Epar.Sbj = parameters.SUBJECT;
            self.Epar.Exp = parameters.EXPERIMENT;
            self.FpC(1) = parameters.FPC;
            self.randomFixation(1) = parameters.FIXATION;
            self.occluder.X(1) = parameters.OCC_X;
            self.occluder.W(1) = parameters.OCC_W;
            self.jump.frame(1) = parameters.JUMP_FRAME;
            self.jump.m(1)     = parameters.JUMP_dPHASE;
            % Stimuli function

            if ~isempty(parameters.STIMULI)
                self.stimuli = inline(parameters.STIMULI,'x','f');
            end
            self.edffile       = edfFileInstance;
            
        end
        
        
        function plotTrial(self)

            hp_stimuli = plotTrial@ET_GEN(self,1);
            hp_eyes = self.edffile.plotEyes(self.resample/self.scn.refresh_rate);
            legend([hp_stimuli,hp_eyes],'Stimuli','Right eye','location',[.8,.8,.1,.1]);
            title(sprintf('X-coordinates. Frames per Cycle: %0.2f',self.FpC(1)))
        end
        
        function [v,s] = velocity(self)
           % Get signal velocity
           s = self.edffile.signal();
           % Reduce the signal to degrees
           s = self.edffile.interpolateSignal(s,'linear');
           s = s(~isnan(s));
           s = self.edffile.filter(s,20,1000); % With defaults !!!!!!!!!!!!!!!!!
           
           angles = inline('atan((s - self.scn.w/2) * (self.scn.wcm/self.scn.w)/self.view_dist)*(180/pi)','s','self');
           s = angles(s,self);
           s = gradient(s)*self.resample;
           s = s(:);
           % Get real velocity
           v = self.returnVectors(1);
           v = angles(v,self);
           v = gradient(v)*self.resample;
           v = v(:);
        end
        
        function phase = phaseMLE(self)
           s = self.edffile.signal();
           s = self.edffile.interpolateSignal(s,'spline');
           self.randomFixation(1)
           s = (s - self.scn.w/2)/((self.scn.w/2)*self.randomFixation(1));
           s = self.edffile.filter(s,20,1000); % With defaults
           s = gradient(s);
           keyboard
           fprintf(1,'First value %0.4f\n',s(1));
           phase = zeros(self.cycles,1);
           n0 = 1;
           for i = 1:self.cycles
               t = round(1000*(self.FpC(1)/self.refresh_rate)-1);
               try
                      phase(i) = self.edffile.phaseMLE(s(n0:n0+t),self.FpC(1),self.resample);
               catch
                      phase(i) = self.edffile.phaseMLE(s(n0:numel(s)),self.FpC(1),self.resample);
               end
               n0 = n0 + t;
           end
        end

        function plotVelocity(self)
            [v,s] = self.velocity();
            % For filters with a phase change....
            % offset = max(numel(self.edffile.transferFunction.zeros),...
            % numel(self.edffile.transferFunction.poles));
            offset = 1;
            y1 = max(max(v(offset:end)),max(s(offset:end))) * 1.2;
            y0 = min(min(v(offset:end)),min(s(offset:end))) * 1.2;
            self.plotOccluder(1,y0,y1);
            h1 = plot((offset:numel(v)),v(offset:end),'k'); hold on
            h2 = plot((offset:numel(s)),s(offset:end),'r');
            plot((1:numel(s)),zeros(numel(s),1),':b');
            legend([h1,h2],'Stimuli','Eye')
            ylim([y0,y1])
            title('Velocity')
            xlabel('Millisecs')
            ylabel('Degrees per second')
       
        end
        
        function [v,s,ox,ow] = normalizedData(self)
            % [v,s,ox,ow] = normilizedData(self)
            % Returns a normalized version of the data ready for being
            % fitted into the model. The units are normalized to unit
            % amplitude and the sampling rate of the stimuli.
            %
            %
            % v         original stimuli.
            % s         recorded signal interpolated, filtered, and.
            %           downsampled.
            % ox        center of the occluder.
            % ow        width of the occluder.
            
            [s p0] = self.edffile.rawSignal();
            st = self.edffile.start();
            f  = self.edffile.final();
            
            v = self.returnVectors(1);
            
            s = self.edffile.interpolateSignal(s(st{1}{1}-p0:f{1}{1}-p0),'linear');
            s = self.edffile.filter(s,self.scn.refresh_rate/2,self.resample);
            % This is very sloppy
            s = s(round(1:self.resample/self.scn.refresh_rate:numel(s))); 
            
            v = (v-self.scn.w/2)/(self.scn.w/2);
            s = (s-self.scn.w/2)/(self.scn.w/2);
            v = (v/self.randomFixation());
            s = (s/self.randomFixation());
            t = min(numel(v),numel(s));
            
            v = v(1:t);
            s = s(1:t)';
            
            ox = self.occluder.X;
            ow = self.occluder.W;
            
            ox = (ox)/(self.scn.w/2);
            ow = (ow)/(self.scn.w/2);
            ox = (ox/self.randomFixation());
            ow = (ow/self.randomFixation());
            
        end

    end

end